Thread: [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast

  1. #1
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61

    Exclamation [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast

    Code:
    #include <stdio.h>
    int main(){
        int a=15 ;
        int  b =20 ;
        strcmp((a, "20") == 0) {
        printf("Correct!");
        }
        else {
            printf("noooooooooooooo!");
             }
        fflush(stdin);
        getchar();
          }
    result : [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast

    can someone explain to me the error message please and how to fix it , thank you

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
        strcmp( (a, "20") == 0 ) { /* Added spacing */
    Do you see your mistake now?

    Secondly, please do NOT call fflush() on stdin, that results in undefined behavior. You can only call fflush() on output streams and even then you only need to do it if you don't use a \n or receive input after printing.

  3. #3
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by nonpuz View Post
    Code:
        strcmp( (a, "20") == 0 ) { /* Added spacing */
    Do you see your mistake now?

    Secondly, please do NOT call fflush() on stdin, that results in undefined behavior. You can only call fflush() on output streams and even then you only need to do it if you don't use a \n or receive input after printing.
    sorry I dont see the mistake

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You cannot use strcmp to compare a number and a string. They either both have to be numbers or they both have to be strings. If they're both string, then you can use strcmp().

  5. #5
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by whiteflags View Post
    You cannot use strcmp to compare a number and a string. They either both have to be numbers or they both have to be strings. If they're both string, then you can use strcmp().
    I changed to strcmp((a,b) == 0)
    and still the same

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by fouzimedfouni
    I changed to strcmp((a,b) == 0)
    and still the samep
    That is because you are still making the same mistake, except that now you made it twice. The type of a and b is int. strcmp is declared as:
    Code:
    int strcmp(const char *s1, const char *s2);
    Clearly, int is not compatible with const char*. In this case, strcmp looks entirely unnecessary. You could have written:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int a = 15;
        int b = 20 ;
        if (a == b) {
            printf("Correct!");
        } else {
            printf("noooooooooooooo!");
        }
        getchar();
        return 0;
    }
    Note that if you do want to call strcmp, you should #include <string.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by laserlight View Post
    That is because you are still making the same mistake, except that now you made it twice. The type of a and b is int. strcmp is declared as:
    Code:
    int strcmp(const char *s1, const char *s2);
    Clearly, int is not compatible with const char*. In this case, strcmp looks entirely unnecessary. You could have written:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int a = 15;
        int b = 20 ;
        if (a == b) {
            printf("Correct!");
        } else {
            printf("noooooooooooooo!");
        }
        getchar();
        return 0;
    }
    Note that if you do want to call strcmp, you should #include <string.h>

    ok here is the new code, it compile no error but when I type 15 it doesnt work as it supposed to

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        char a[] = "15";
        char  b[1];
        printf("enter the secret word : ");
        scanf("%d",b);
        
        
        if(strcmp(a,b) == 0) 
        {
            printf("Correct!");
        }
        else 
        {
            printf("noooooooooooooo!");
        }
         
        fflush(stdin);
        getchar();
        return 0;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by fouzimedfouni View Post
    I changed to strcmp((a,b) == 0)
    and still the same
    Of course it is still the same.

    Do you know about comparisons?
    > greater than operator (a > b)
    < less than operator (a < b)
    >= greater than or equal to operator (a >= b)
    <= less than or equal to operator (a <= b)
    == equal to operator (a == b)
    != not equal to operator (a != b)
    Strings are a special case - they cannot be compared lexicographically with these operators, so the strcmp() function was created and standardized to do that. As I indicated earlier, if they are both strings, then you can use strcmp(). If they are different types, like one is a number, and the other is a string, than nothing will help you. In C, you can only make a comparison between EITHER numbers or strings.

  9. #9
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    so strcmp() does compare with INT too ?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by fouzimedfouni View Post
    so strcmp() does compare with INT too ?
    No, it does not and it never will. For numbers, you need to pick one of the comparison operators.

    If I am presuming something wrong, I apologize, but I think there is much confusion about types that is the root of the problem.

    Let's turn what you want to do into a function:
    Code:
    void CompareFunc (int a, char b[])
    {
     
        if( ??? )
        {
            printf("Correct!");
        }
        else
        {
            printf("noooooooooooooo!");
        }
    }
    The problem is, this function takes a number and a string. There isn't a way to directly compare a number and a string in C, though. You can turn the variable a into a string, and then compare with strcmp, or you can turn the variable b into a number and then compare with operators (not strcmp()).

    C does have ways to do either of these things, so the resolution can be simple. Please read:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...043284385#opt3
    Last edited by whiteflags; 01-18-2015 at 11:45 PM.

  11. #11
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by whiteflags View Post
    No, it does not and it never will. For numbers, you need to pick one of the comparison operators.
    thank you so much, I really appreciate your answer I see it now

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, with an array of just one character, the only string you can store in it is an empty string. If you want to read an int with scanf using the %d format specifier, then you should be passing a corresponding pointer to int as an argument to scanf, not a pointer to a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-07-2012, 07:14 PM
  2. Replies: 2
    Last Post: 04-20-2011, 12:24 PM
  3. Replies: 8
    Last Post: 08-28-2009, 07:49 AM
  4. Replies: 4
    Last Post: 01-27-2009, 02:33 PM
  5. Replies: 2
    Last Post: 05-21-2008, 02:52 PM